package com.compass.util;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Field;


/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/4/18
 * Time: 22:18
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
public class CopyProperties {
    /**
     * 返回页面的model，放入到modelAndView里面
     *
     * @param source
     * @param modelAndView
     **/
    public static void copyToModel(Object source, ModelAndView modelAndView) {
        if (null != source) {
            Field[] fields = FieldUtils.getAllFields(source.getClass());
            for (Field field : fields) {
                field.setAccessible(true);
                Object o = ReflectionUtils.getField(field, source);
                if (null != o) {
                    String name = field.getName();
                    modelAndView.addObject(name, o);
                }
            }
        }
    }
}
